<template>
    <div class="article-lists">
       <el-card class="!border-none" shadow="never">

            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="所属城市">
                    <el-select-v2
                        v-model="queryParams.area_id"
                        filterable
                        clearable
                        :options="optionCity"
                        placeholder="请选择"
                        class="w-[280px]"
                        :height="280"
                    />
				</el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" shadow="never">
            <div>
                <router-link
                    v-perms="['office.office_place/add', 'office.office_place/add:edit']"
                    :to="{
                        path: getRoutePath('office.office_place/add:edit')
                    }"
                >
                    <el-button type="primary" class="mb-4">
                        <template #icon>
                            <icon name="el-icon-Plus" />
                        </template>
                        新增场地
                    </el-button>
                </router-link>
            </div>
            <el-table size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="序号(ID)" prop="id" min-width="120" />
                <el-table-column label="所属城市" prop="area_id" min-width="110" >
					<template #default="{ row }">
						<span v-if="row.area_id" v-city="row.area_id"></span>
						<span v-else>平台</span>
					</template>
				</el-table-column>
                <el-table-column label="场地名称" min-width="180" prop="name"/>
                <el-table-column label="场地类型" min-width="140" prop="type">
                    <template #default="{row}">
                        <span v-if="row.type == 1">签约洽谈中心</span>
                        <span v-if="row.type == 2">共享办公</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="位置"
                    prop="location_name"
                    min-width="280"
                    show-tooltip-when-overflow
                />

                <el-table-column label="面积" min-width="160" show-tooltip-when-overflow>
                  <template #default="{row}">
                      {{ row?.size +'m²'}}
                  </template>
                </el-table-column>
                <el-table-column label="联系电话" prop="link_phone" min-width="140" />
                <el-table-column label="IM联系人员" prop="im_service_name" min-width="180" />
                <el-table-column label="状态" prop="is_enable_desc" min-width="100" />
                <el-table-column label="创建时间" prop="create_time" min-width="180" />
                <el-table-column label="操作" width="240" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['office.office_place/edit', 'office.office_place/add:edit']"
                            type="primary"
                            link
                        >
                            <router-link
                                :to="{
                                    path: getRoutePath('office.office_place/add:edit'),
                                    query: {
                                        id: row.id
                                    }
                                }"
                            >
                                编辑
                            </router-link>
                        </el-button>
                        <!-- <router-link
                            :to="{
                                path: getRoutePath('office.office_place/adminList'),
                                query: {
                                    id: row.id
                                }
                            }"
                        >
                            <el-button v-perms="['office.office_place/adminList']" type="primary" link >账号列表</el-button>
                        </router-link> -->
                        <el-button type="primary" link @click="addLink(row.link_phone)">分享后台</el-button>
                        <el-button
                            v-perms="['office.office_place/delete']"
                            type="danger"
                            link
                            @click="handleDelete(row.id, row.area_id)"
                        >
                            删除
                        </el-button>

                        <el-switch
                            v-perms="['office.office_place/updateStatus']"
                            v-model="row.is_enable"
                            :active-value="1"
                            :inactive-value="0"
                            @change="changeStatus($event, row.id)"
                        />
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <el-dialog v-model="recordVisible" title="分享后台" width="600">
            <div class="flex-link mb-[10px]">后台地址：{{ link }}<el-link class="flex-m" type="primary" icon="el-icon-document-copy" :underline="false" v-copy="link">复制</el-link></div>
            <div class="flex-link mb-[10px]">登录账号：{{ link_phone }}</div>
            <div class="flex-link">登录密码：{{ link_phone }}</div>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="()=>{recordVisible = false}">取消</el-button>
                    <el-button v-perms="['user.agent/setBondConfig']" type="primary" v-copy="link">复制链接</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>
<script lang="ts" setup name="officePlaceLists">
import {officePlacelists, officePlaceDelete, officePlaceStatus} from '@/api/office/officePlace'
import { usePaging } from '@/hooks/usePaging'
import { getRoutePath } from '@/router'
import feedback from '@/utils/feedback'

const queryParams = reactive({
    name: '',
    is_enable: '',
    start_time: '',
    end_time: '',
    area_id: ''
})
import useAppStore from '@/stores/modules/app'
const useApp = useAppStore()
const optionCity:any = useApp.optionCity;


const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: officePlacelists,
    params: queryParams
})
const changeStatus = async (is_enable: any, id: number) => {
    try {
        await officePlaceStatus({ id, is_enable })
        getLists()
    } catch (error) {
        getLists()
    }
}

const handleDelete = async (id: number, area_id:number|string) => {
    await feedback.confirm('确定要删除？')
    await officePlaceDelete({ id, area_id  })
    getLists()
}

onActivated(() => {
    getLists()
})
const link = ref('https://api.onestep6.com/office');
const recordVisible = ref(false);
const link_phone:any = ref('')
const addLink = (phone:any) => {
    link_phone.value = phone;
    recordVisible.value = true;
}

getLists()
</script>
